package com.test26_比较器;

import java.util.Arrays;

/**
 * Comparable比较器
 * 设计时考虑到排序需求
 */
public class Demo02_Comparable {

    public static void main(String[] args) {
        Book2[] books = new Book2[] {
                new Book2("Java从入门到项目实战", 99.8),
                new Book2("Python从入门到项目实战", 89.7),
                new Book2("Go语言从入门到项目实战", 96.3)
        };
        Arrays.sort(books); // 数组排序处理
        Arrays.stream(books).forEach(System.out::println);
    }
}

class Book2 implements Comparable<Book2> {
    private String title;
    private double price;

    public Book2(String title, double price) {
        this.title = title;
        this.price = price;
    }

    @Override
    public String toString() {
        return "【Book】图书名称：" + this.title + "、图书价格：" + this.price;
    }

    @Override
    public int compareTo(Book2 o) {
        if (this.price > o.price) {
            return 1;
        } else if (this.price < o.price) {
            return -1;
        }
        return 0;
    }
}
